//
//  WHTabBarControllerConfig.m
//  LvZhongMagicKey
//
//  Created by 邓伟浩 on 2017/12/14.
//  Copyright © 2017年 邓伟浩. All rights reserved.
//

#import "WHTabBarControllerConfig.h"

#import "WHHomeController.h"
#import "WHWalletController.h"
#import "WHMineController.h"

#import "WHRootNavigationController.h"

@interface WHTabBarControllerConfig ()<UITabBarControllerDelegate>

@property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;

@end

@implementation WHTabBarControllerConfig

- (CYLTabBarController *)tabBarController {
    if (_tabBarController == nil) {
        /**
         * 以下两行代码目的在于手动设置让TabBarItem只显示图标，不显示文字，并让图标垂直居中。
         * 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
         * 更推荐后一种做法。
         */
        UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
        UIOffset titlePositionAdjustment = UIOffsetZero;//UIOffsetMake(0, MAXFLOAT);
        
        CYLTabBarController *tabBarController = [CYLTabBarController
                                                 tabBarControllerWithViewControllers:self.viewControllers
                                                                                   tabBarItemsAttributes:self.tabBarItemsAttributesForController
                                                                                             imageInsets:imageInsets
                                                                                 titlePositionAdjustment:titlePositionAdjustment];
        [self customizeTabBarAppearance:tabBarController];
        _tabBarController = tabBarController;
    }
    
    return _tabBarController;
}

- (NSArray *)viewControllers {
    WHHomeController *homeController = [[WHHomeController alloc] init];
    UIViewController *homeNavi = [[WHRootNavigationController alloc]
                                  initWithRootViewController:homeController];
    
    WHWalletController *myWalletController = [[WHWalletController alloc] init];
    UIViewController *myWalletNavi = [[WHRootNavigationController alloc]
                                     initWithRootViewController:myWalletController];
    
    
    WHMineController *mineController = [[WHMineController alloc] init];
    UIViewController *mineNavi = [[WHRootNavigationController alloc]
                                  initWithRootViewController:mineController];
    
    
    NSArray *viewControllers = @[
                                 homeNavi,
                                 myWalletNavi,
                                 mineNavi
                                 ];
    
    return viewControllers;
}

- (NSArray *)tabBarItemsAttributesForController {
    NSDictionary *dict1 = @{
                            CYLTabBarItemTitle : @"首页",
                            CYLTabBarItemImage : @"tabbar_home_unselected",
                            CYLTabBarItemSelectedImage : @"tabbar_home_selected",
                            };
    NSDictionary *dict2 = @{
                            CYLTabBarItemTitle : @"钱包",
                            CYLTabBarItemImage : @"tabbar_myWallet_unselected",
                            CYLTabBarItemSelectedImage : @"tabbar_myWallet_selected",
                            };
    NSDictionary *dict3 = @{
                            CYLTabBarItemTitle : @"我的",
                            CYLTabBarItemImage : @"tabbar_mine_unselected",
                            CYLTabBarItemSelectedImage : @"tabbar_mine_selected",
                            };
    
    NSArray *tabBarItemsAttributes = @[dict1, dict2, dict3];
    return tabBarItemsAttributes;
}

- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
//    tabBarController.tabBarHeight = CYL_IS_IPHONE_X ? 65 : 40;
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = CMDarkOrangeColor;
    
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
}

@end
